import datetime
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base, str_150


class Book(Base):
    __tablename__ = 'books'

    title: Mapped[str_150]
    count: Mapped[int] = mapped_column(default=1)
    release_date: Mapped[datetime.date]
    author_id: Mapped[int] = mapped_column(ForeignKey("authors.id", ondelete="CASCADE"))

    def __repr__(self):
        return f"<Book(id={self.id}, title={self.title}>)"

    def __getitem__(self, item):
        return getattr(self, item)

    def to_json(self):
        to_json = {c.name: f'{getattr(self, c.name)}' for c in self.__table__.columns}
        return to_json
